<?php
class LoginController extends Zend_Controller_Action {
	public function loginpostAction() {
		if (count($_POST) <= 0) {
			$this->_redirect('/');
		}
		
		/* Validate form */
		$validators = array(
			'username' => 'Alnum',
			'key' => array(
				'Alnum',
				new Zend_Validate_StringLength(13, 128),
			),
			'password' => array('presence' => 'required'),
		);
		$input = new Zend_Filter_Input(NULL, $validators, $_POST);
		if ($input->hasInvalid() || $input->hasMissing()) {
			$this->view->message = $input->getMessages();
		}
		else {
			$auth = Zend_Auth::getInstance();
			$authAdapter = new NerdPass_Auth($_POST['username'], $_POST['password'], $_POST['key']);
			$result = $auth->authenticate($authAdapter);
			if (!$result->isValid()) {
				foreach ($result->getMessages() as $message) {
					$this->view->message = $message;
				}
			}
			else {
				$this->_redirect('/');
			}
		}
		$this->_forward('login', 'login');
	}
	
	public function loginAction() {
		$this->view->notloggedin = true;
	}
	
	public function logoutAction() {
		Zend_Auth::getInstance()->clearIdentity();
		$this->_redirect('/');
	}
}